﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _08_Lambda表达_有参有返回值
{

    public delegate int OtherDel(int intParam);

    internal class Program
    {

        public static int Add(int x)
        {
            return x + 1;
        }

        static void Main(string[] args)
        {
            //实例化委托=>绑定方法
            OtherDel otherDel = new OtherDel(Add);
            //调用委托对象
            Console.WriteLine("通过实例化委托绑定:{0}",otherDel.Invoke(1));

            //通过匿名的方式实例化委托
            OtherDel otherDel2 = delegate (int x)
            {
                return x + 1;
            };
            Console.WriteLine("通过匿名的方式实例化委托:{0}", otherDel2.Invoke(1));

            //Lambda表达式-1
            OtherDel otherDel3 = (int x) => { return x + 1; };
            Console.WriteLine("通过Lambda表达式实例化委托:{0}", otherDel3.Invoke(1));

            //Lambda表达式-2
            OtherDel otherDel4 = (x) => { return x + 1; };
            Console.WriteLine("通过Lambda表达式实例化委托:{0}", otherDel4.Invoke(1));

            //Lambda表达式-3 (参数有且只有一个，可以省略括号)
            OtherDel otherDel5 = x => { return x + 1; };
            Console.WriteLine("通过Lambda表达式实例化委托:{0}", otherDel5.Invoke(1));

            //Lambda表达式-4 (如果方法体只有一行代码，可以省略大括号)
            OtherDel otherDel6 = x => x + 1;
            Console.WriteLine("通过Lambda表达式实例化委托:{0}", otherDel6.Invoke(1));


            Console.Read();

        }
    }
}
